package com.xjrsoft.module.erpModel.caseErpSupplier.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.YesOrNoEnum;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.module.erpModel.caseErpSupplier.dto.*;
import com.xjrsoft.module.erpModel.caseErpSupplier.entity.CaseErpSupplier;
import com.xjrsoft.module.erpModel.caseErpSupplier.entity.CaseErpSupplierRisk;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplierFormalService;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplierRiskService;
import com.xjrsoft.module.erpModel.caseErpSupplier.service.ICaseErpSupplierService;
import com.xjrsoft.module.erpModel.caseErpSupplier.vo.CaseErpSupplierInfoVo;
import com.xjrsoft.module.erpModel.caseErpSupplier.vo.CaseErpSupplierRiskInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 供应商信息【case_erp_supplier】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-06-25
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_SUPPLIER + "/caseErpSupplier")
@Api(value = GlobalConstant.CASE_ERP_SUPPLIER + "/caseErpSupplier", tags = "供应商配置")
@AllArgsConstructor
public class CaseErpSupplierController {
    private ICaseErpSupplierService caseErpSupplierService;

    private ICaseErpSupplierFormalService caseErpSupplierFormalService;

    private ICaseErpSupplierRiskService caseErpSupplierRiskService;

    @GetMapping(value = "/page")
    @ApiOperation("供应商分页")
    public R page(CaseErpSupplierPageDto dto) {
        return R.ok(caseErpSupplierService.getPageList(dto));
    }

    @GetMapping("/list")
    @ApiOperation(value = "供应商列表，不分页")
    public R list() {
        List<CaseErpSupplier> list = caseErpSupplierService.list(Wrappers.<CaseErpSupplier>query().lambda()
                .ne(CaseErpSupplier::getFormalState,0 )   //0未年审
                .orderByDesc(CaseErpSupplier::getCreateDate));
        return R.ok(list);
    }

    @GetMapping("/codeNumber")
    @ApiOperation(value = "返回编码代号和流水号")
    public R getCodeNumber() {
        return R.ok(caseErpSupplierService.getCodeNumber());
    }

    @PostMapping
    @ApiOperation(value = "新增潜在供应商")
    public R add(@Valid @RequestBody AddCaseErpSupplierDto dto) throws JsonProcessingException {
        if (dto.getIsSysNumBoolean()) {
            dto.setIsSysNum(YesOrNoEnum.YES.getCode());
        } else {
            dto.setIsSysNum(YesOrNoEnum.NO.getCode());
        }
        return R.ok(caseErpSupplierService.add(dto));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询供应商信息")
    public R info(@RequestParam Long id) {
        CaseErpSupplier caseErpSupplier = caseErpSupplierService.getById(id);
        if (caseErpSupplier == null) {
            R.error("找不到此供应商！");
        }
        CaseErpSupplierInfoVo caseErpSupplierInfoVo = caseErpSupplierService.info(caseErpSupplier);
        return R.ok(caseErpSupplierInfoVo);
    }

    @PutMapping
    @ApiOperation(value = "修改潜在供应商")
    public R update(@Valid @RequestBody UpdateCaseErpSupplierDto dto) {
        if (dto.getIsSysNumBoolean()) {
            dto.setIsSysNum(YesOrNoEnum.YES.getCode());
        } else {
            dto.setIsSysNum(YesOrNoEnum.NO.getCode());
        }
        CaseErpSupplier caseErpSupplier = BeanUtil.toBean(dto, CaseErpSupplier.class);
        caseErpSupplierService.updateById(caseErpSupplier);
        return R.ok(true);
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @XjrLog(value = "删除供应商")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpSupplierService.removeById(id));
    }


    @PostMapping("/confirmation")
    @ApiOperation(value = "供应商转正申请")
    public R confirmation(@Valid @RequestBody AddCaseErpSupplierFormalDto dto) throws JsonProcessingException {
        return R.ok(caseErpSupplierFormalService.confirmation(dto));
    }

    @GetMapping("/confirmation-info")
    @ApiOperation("详情查看（转正与时间轴部分）")
    public R getConfirmationInfo(@RequestParam Long id) {
        return R.ok(caseErpSupplierFormalService.getConfirmationInfo(id));
    }

    @PostMapping("/add-risk")
    @ApiOperation(value = "新增风险评估")
    public R addRisk(@Valid @RequestBody AddCaseErpSupplierRiskDto dto) throws JsonProcessingException {
        return R.ok(caseErpSupplierService.addRisk(dto));
    }

    @GetMapping(value = "/risk-info")
    @ApiOperation(value = "根据供应商id查询风险评估信息（正常评估）")
    public R riskInfo(@RequestParam Long id) {
        CaseErpSupplier caseErpSupplier = caseErpSupplierService.getById(id);
        if (caseErpSupplier == null) {
            R.error("找不到此供应商信息！");
        }
        CaseErpSupplierInfoVo caseErpSupplierInfoVo = caseErpSupplierService.info(caseErpSupplier);
        CaseErpSupplierRiskInfoVo caseErpSupplierRiskInfoVo = new CaseErpSupplierRiskInfoVo();
        caseErpSupplierRiskInfoVo.setCaseErpSupplierInfoVo(caseErpSupplierInfoVo);
        List<CaseErpSupplierRisk> caseErpSupplierRiskList = caseErpSupplierRiskService.list(Wrappers.<CaseErpSupplierRisk>query().lambda()
                .eq(CaseErpSupplierRisk::getSupplierId, id)
                .orderByDesc(CaseErpSupplierRisk::getCreateDate));
        if (CollectionUtil.isNotEmpty(caseErpSupplierRiskList)) {
            R.error("找不到此风险评估信息！");
        }
        CaseErpSupplierRisk caseErpSupplierRisk = caseErpSupplierRiskList.get(0);
        caseErpSupplierRiskInfoVo.setCaseErpSupplierRisk(caseErpSupplierRisk);
        return R.ok(caseErpSupplierRiskInfoVo);
    }

    @GetMapping(value = "/year-info")
    @ApiOperation(value = "根据供应商id查询风险评估信息（年审评估）")
    public R yearRiskInfo(@RequestParam Long id) {
        CaseErpSupplier caseErpSupplier = caseErpSupplierService.getById(id);
        if (caseErpSupplier == null) {
            R.error("找不到此供应商信息！");
        }
        CaseErpSupplierInfoVo caseErpSupplierInfoVo = caseErpSupplierService.info(caseErpSupplier);
        CaseErpSupplierRiskInfoVo caseErpSupplierRiskInfoVo = new CaseErpSupplierRiskInfoVo();
        caseErpSupplierRiskInfoVo.setCaseErpSupplierInfoVo(caseErpSupplierInfoVo);
        List<CaseErpSupplierRisk> caseErpSupplierRiskList = caseErpSupplierRiskService.list(Wrappers.<CaseErpSupplierRisk>query().lambda()
                .eq(CaseErpSupplierRisk::getSupplierId, id)
                        .eq(CaseErpSupplierRisk::getType,YesOrNoEnum.YES.getCode())
                .orderByDesc(CaseErpSupplierRisk::getCreateDate));
        if (CollectionUtil.isNotEmpty(caseErpSupplierRiskList)) {
            R.error("找不到此年审信息！");
        }
        CaseErpSupplierRisk caseErpSupplierRisk = caseErpSupplierRiskList.get(0);
        caseErpSupplierRiskInfoVo.setCaseErpSupplierRisk(caseErpSupplierRisk);
        return R.ok(caseErpSupplierRiskInfoVo);
    }

    @GetMapping(value = "/detail-year-info")
    @ApiOperation(value = "根据riskId查询风险评估信息（详情查看里面的查看年审报告使用）")
    public R detailYearRiskInfo(@RequestParam Long id) {
        CaseErpSupplierRisk caseErpSupplierRisk = caseErpSupplierRiskService.getById(id);
        if (caseErpSupplierRisk == null){
            R.error("找不到此年审信息！");
        }
        CaseErpSupplier caseErpSupplier = caseErpSupplierService.getById(caseErpSupplierRisk.getSupplierId());
        if (caseErpSupplier == null) {
            R.error("找不到此供应商信息！");
        }
        CaseErpSupplierInfoVo caseErpSupplierInfoVo = caseErpSupplierService.info(caseErpSupplier);
        CaseErpSupplierRiskInfoVo caseErpSupplierRiskInfoVo = new CaseErpSupplierRiskInfoVo();
        caseErpSupplierRiskInfoVo.setCaseErpSupplierInfoVo(caseErpSupplierInfoVo);
        caseErpSupplierRiskInfoVo.setCaseErpSupplierRisk(caseErpSupplierRisk);
        return R.ok(caseErpSupplierRiskInfoVo);
    }

    @PutMapping("/supplier-out")
    @ApiOperation("供应商淘汰")
    @SneakyThrows
    public R outSupplier(@RequestBody SupplierOutDto dto) {
        return R.ok(caseErpSupplierService.outSupplier(dto));
    }

    @PutMapping("/supplier-recover")
    @ApiOperation("供应商恢复")
    @SneakyThrows
    public R recoverSupplier(@RequestBody SupplierRecoverDto dto) {
        return R.ok(caseErpSupplierService.recoverSupplier(dto));
    }

    @GetMapping("/type")
    @ApiOperation(value = "获取供应商类型分布")
    public R getSupplierType() {
        return R.ok(caseErpSupplierService.getSupplierType());
    }

    @GetMapping("/risk")
    @ApiOperation(value = "潜在供应商风险评估")
    public R risk() {
        return R.ok(caseErpSupplierService.risk());
    }


    @GetMapping("/history")
    @ApiOperation(value = "历史价格对比")
    public R history() {
        return R.ok(caseErpSupplierService.historyPrice());
    }

}
